package java4_6.Homework1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import java.util.List;

public class Server extends Thread{
    private final Socket s;
    List<User> list = new ArrayList<>();
    {
        list.add(new User("xiaolin","1234"));
        list.add(new User("xbl","1234"));
    }
    public Server(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        System.out.println(s+"连接了");
        try {
            InputStream input = s.getInputStream();
            OutputStream out = s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(input));
            PrintWriter pw=new PrintWriter(out,true);
            String str1 = br.readLine();
            String str2 = br.readLine();
                for (User user : list) {
                    if (user.getId().equals(str1)||user.getId().equals(str2)){
                        System.out.println("客户端："+s.getInetAddress()+"正在登录");
                        pw.println("登录成功");
                        break;
                    }else {
                        pw.println("账号或密码错误！");
                    }
                }
                s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            ServerSocket so1 = new ServerSocket(5253);
            while (true) {
                Socket s = so1.accept();
                new Server(s).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
